package com.game;


import android.content.Context;


import android.graphics.Canvas;

import android.os.Bundle;
import android.util.Log;

import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class GaismaThread extends Thread {
	
	private SurfaceHolder 		mSurfaceHolder;
	private Context 			mContext;
	
	private SceneManager 		mSceneManager;
	
	public int 					resolutionWidth;
	public int 					resolutionHeight;
	
	private boolean 			mRunning;
	private long  				oldTime;
	private long 				oldLogicTime;
	private int 				fps;
	private int 				logicTimestep;
	
	// Constant for the logic speed
	public final int 			FPSspeed = 30;
	
	
	private boolean 			gamePaused;
	public boolean  			m_bSaveGameFound;
	
	
	
	
	
	public GaismaThread(SurfaceHolder surface, Context context)
	{		
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::Constructor" );
		
		// Keep a pointer to surface (drawing) and context (loading)
		mSurfaceHolder = surface;
		mContext = context;
		
		mSceneManager = new SceneManager(this);
		mSceneManager.ChangeScene(new Scene0(mSceneManager));
				
		oldTime = 0;
		oldLogicTime = 0;
		
		logicTimestep = 1000/FPSspeed ;		
		
		gamePaused = false;		
		m_bSaveGameFound = true;
		
		// Hack: Need to call this from the real loading point
		GameData.getGameData().LoadData();
	}
	
	
	
	public boolean handleTouch(MotionEvent event)
	{
		return mSceneManager.handleTouch(event);
	}
	
	
	public void doStart()
	{
		// To start new game
		
	}
	
	
	

	public void pause()
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::pause" );
		gamePaused =  false;
	}
	
	public synchronized void restoreState(Bundle savedState)
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::restoreState" );
		
		// To resume a previous game
		
		//currentLevel = savedState.getInt("CurrentLevel");
		//StartLevel();		
	}
	
	public Bundle saveState(Bundle map)
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::saveState" );
		
		// To save game
		
		//map.putInt("CurrentLevel", currentLevel);
		return map;
	}
	
	@Override public void run()
	{
		while(mRunning)
		{
			Canvas c = null;
			try
			{
				c = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder) 
				{
					long curTime = System.currentTimeMillis();
					
					
					// FPS calculations
					fps++;
					long elapsed = curTime - oldTime;
					if( elapsed >= 1000)
					{
						Log.i("GAISMA LOG:: ", " FPS:  " + fps);
						
						//mBackgroundMirror.setGameFPS(fps);
						oldTime = curTime;
						fps = 0;						
					}
					
					// Fixed logic update at FPSspeed (as long as FPSspeed < fps)
					long logicElapsed = curTime - oldLogicTime;
					if(logicElapsed >= logicTimestep)
					{
						
						if(!gamePaused) update();
						oldLogicTime = curTime;				
						
					}
					
					// Draw at max speed.
					doDraw(c);
				
				}
			}
			finally
			{
				if(c!=null)
				{
					mSurfaceHolder.unlockCanvasAndPost(c);
				}
			}
		}
	}
	
	
	
	public void setSurfaceSize(int width, int height)
	{
		
	}
	
	public void unPause()
	{
		gamePaused = false;
	}
	
	public void startGame()
	{
		
	}
	
	public void setScreenResolution(int width, int height)
	{
		resolutionHeight = height;
		resolutionWidth = width;
		
	}
	
	private void doDraw (Canvas canvas)
	{
		mSceneManager.Draw(canvas);		
	}
	
	private void update()
	{		
		
		mSceneManager.Update();
	}

	public void setmRunning(boolean mRunning) {
		this.mRunning = mRunning;
	}

	public boolean ismRunning() {
		return mRunning;
	}
	
	public Context getContext() { return mContext;}
	public SurfaceHolder getSurface() {return mSurfaceHolder;}
	
}
